package com.zjuee.web.auth.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.model.common.auth.Org;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Update;
import com.zjuee.service.auth.service.OrgService;
import com.zjuee.service.auth.vo.OrgVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 组织 web 接口
 *
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(path = Common.Service.ZJUEE_AUTH_ORG_URL_PREFIX, name = Common.Service.ZJUEE_AUTH_SERVICE_NAME)
public class OrgController {

    @Resource
    private OrgService orgService;


    //添加组织机构
    @PostMapping("/add")
    public R<Long> add(@Validated(Insert.class) @RequestBody OrgVo org) {
        try {
            Long add = orgService.add(org);
            if (null != add) {
                return R.ok(add);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //修改组织机构
    @PutMapping("/update")
    public R<Boolean> updateOrg(@Validated(Update.class) @RequestBody Org org) {
        try {
            Boolean update = orgService.update(org);
            if (update) {
                return R.ok(update);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //删除组织机构
    @DeleteMapping("/delete/{orgId}")
    public R<Boolean> deleteOrg(@PathVariable("orgId") Long orgId) {
        try {
            Boolean delete = orgService.delete(orgId);
            if (delete) {
                return R.ok(delete);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //仅获取政府类型的组织机构（树形式）
    @GetMapping("/gov/org")
    public R<List<OrgVo>> govOrg() {
        try {
            List<OrgVo> orgList = orgService.govOrg();
            if (null != orgList) {
                return R.ok(orgList);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //根据父id，获取此组织机构下一级组织机构
    @GetMapping("/sub/list/{parentId}")
    public R<List<Org>> subList(@PathVariable("parentId") Long parentId) {
        try {
            List<Org> orgList = orgService.subList(parentId);
            if (null != orgList) {
                return R.ok(orgList);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    /**
     * 根据 ID 查询 Org
     *
     * @param id Org Id
     * @return Org
     */
    @GetMapping("/info/{id}")
    public R<Org> selectById(@PathVariable(value = "id") Long id) {
        try {
            Org select = orgService.selectById(id);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
}
